import util from "../../util";
import parentInstance from "../parentInstance";
export default class twins extends parentInstance{
    constructor() {
        super();
        this.util = util;
        this.typeList=['twinsDictionary','twinsQueue','twinsTree','twinsSingleton'];
        this.record = {};
        this.info = null;
        this.pointer = null;
        this.modelMap=null;
        this.hier=null;
        /** modelMap格式
         * {
            table: tableName,
            featureList: new Set(),
            valueList: new Set(),
            fieldList: new Set(),
            groupList: new Set(),
            fieldMap: {},
          }
         */
    }
    static create(record){
        let object=new twins();
        if(!!record){
            object.setRecord(record);
        };
        return object;
    }
    setRecord(record){
        this.record=record;
        if(!record.web){
            this.record.web=twins.webTpl();
            this.record.web.D2.ref.name=this.record.className;
            this.record.web.D3.name=this.record.className;
        }
        this._setPointer();
        this._setHier();
    }
    _setPointer(empty=true){
        if(!empty){
            this.info=null;
            this.pointer=null;
            this.modelMap=null;
            return;
        }
        this.pointer={
            type:this.record.type,
            className:this.record.className,
        };
        this.info={
            type:this.record.type,
            className:this.record.className,
            version:this.record.version,
            published:this.record.published,
        };
        // this.modelMap=this._getModelMap();
    }
    _setHier(hier){
        if(!hier){
            this.hier = this.record.hier;
            return;
        }
        this.hier = this.record.hier = hier;
    }
    _getModelMap(){
        return util.modelState.map.get([this.pointer.type,this.pointer.className].join(','));
    }
    /*******************************************************************************
     * 对象属性编辑功能
     * ******************************************************************************/
    /***
     * 新增
     * @param record
     * @returns {Promise<boolean|*>}
     */
    /*async add(record){
        if(!record||!record.type||!record.className||record.type==''||record.className==''){
            return false;
        }
        let newRecord=Object.assign(twins.tpl(),record);
        let result = await util.api.TwinsInstance.add(newRecord).then(data=>{
            if(!!data){
                this.setRecord(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }*/
    /**
     * 删除
     * @returns {Promise<boolean|*>}
     */
    /*async delete(){
        if(!!this.pointer){
            let result = await util.api.TwinsInstance.delete(this.record.id).then(flag=>{
                if(flag){
                    this.setRecord([]);
                    return true;
                }else{
                    return false;
                }
            }).catch(err=>{
                return false;
            });
            return result;
        }else{
            return false;
        }

    }*/
    /**
     * 更新
     * @returns {Promise<*>}
     */
    async update(){
        let updateRec={
            id: this.record.id,
            type:this.record.type,
            className:this.record.className,
            name:this.record.name,
            version: this.record.version,
            published: this.record.published,
            // features:this.record.features,
            values:this.record.values,
            hier:this.record.hier,
            web:this.record.web,
            // others:this.record.others,
            // createTime: dayjs.localeData(),
            // updateTime: (new Date()).toLocaleDateString(),
            comments: this.record.comments
        }
        let result = await util.api.TwinsInstance.update(updateRec).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 查询
     * @param param
     * @returns {Promise<*>}
     */
    async queryById(id){
        let result = await util.api.TwinsInstance.query({condition:`id=${id}`}).then(data=>{
            if(!!data){
                this.setRecord(data[0]);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async query(filter){
        let result = await util.api.TwinsInstance.query({condition:filter}).then(data=>{
            if(!!data){
                this.setRecord(data[0]);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
	 * 运行时注册,更新hier中某项
	 * @param pointer 注册指针
	 * @param hier  转换成map,如:{"basic":[]}
	 * @returns {*}
	 */
	async runRegister(pointer,hier) {
        let result=await util.api.TwinsInstance.runRegister(pointer,hier).then(data=>{
            if(!!data){
                this._setHier(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
	}

    async runAddRegister(pointer,hier){
        let result = await util.api.TwinsInstance.runAddRegister(pointer,hier).then(data=>{
            if(!!data){
                this._setHier(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }

    async runAllAddRegister(pointer,hier){
        let result = await util.api.TwinsInstance.runAllAddRegister(pointer,hier).then(data=>{
            if(!!data){
                this._setHier(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async runRemoveRegister(pointer,names){
        let result = await util.api.TwinsInstance.runRemoveRegister(pointer,names).then(data=>{
            if(!!data){
                this._setHier(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }

    /**
     * 通过其他模型中的others获取信息
     * @param {*} id
     * @returns
     */
    getOtherFatherById (id) {
        if (!this.hier || this.hier.length === 0) {
            return {}
        }
        this._setPointer();
        let otherInfo = {};
        // 找到节点
        let index = this.hier[0].basic.findIndex(d => String(d.id) === String(id))
        let parentId = '';
        // 找到父节点
        if (index !== -1) {
            parentId = this.hier[0].basic[index].parentId
        }
        let parentIndex = this.hier[0].basic.findIndex(d => String(d.id) === String(parentId))
        if (parentIndex !== -1) {
            otherInfo = this.hier[0].basic[parentIndex].values
        }
        return otherInfo;
    }
    getOtherById (id) {
        if (!this.hier || this.hier.length === 0) {
            return {}
        }
        this._setPointer();
        let otherInfo = {};
        // 找到节点
        let index = this.hier[0].basic.findIndex(d => String(d.id) === String(id))
        if (index !== -1) {
            otherInfo = this.hier[0].basic[index].values
        }
        return otherInfo;
    }
    /**
     * hier的basic是否有值
     */
    isValidHier () {
        if (!this.hier || this.hier.length === 0 || !this.hier[0].basic || this.hier[0].basic.length === 0) {
            return false;
        } else {
            return true
        }
    }
    /**
     * others的basic是否有值
     */
    isValidOthers () {
        if (!this.others || !this.others.basic || this.others.basic.length === 0) {
            return false;
        } else {
            return true
        }
    }
    /**
     * 新增模板
     * @returns {{comments: string, web: {}, values: {}, name: string, className: string, published: boolean, type: string, version: number, hier: *[], others: {}}}
     */
    static tpl(){
        return {
            name: "",
            type:"",
            className:"",
            version: 1,
            published: true,
            values:{},
            hier:[],
            web:{},
            others:{},
            // createTime: dayjs.localeData(),
            // updateTime: (new Date()).toLocaleDateString(),
            comments: ""
        }
    }
    /**可视化*/
    getD2ElementSample(){
        return twins.D2ElementSample();
    }
    addD2Element(name){
        let element=twins.D2ElementItemTpl();
        element.tag=name;
        this.record.web.D2.element.push(element)
    }
    removeD2Element(index){
        this.record.web.D2.element.splice(index,1)
    }
    static webTpl(){
        return {
            D2:twins.D2Tpl(),
            D3:twins.D3Tpl(),
        }
    }
    static D2Tpl(){
        return {
            "ref": {
                "name": "",
                "x": 0,
                "y": 0,
                "width": 200,
                "height": 250
            },
            "element": []
        }
    }
    static D2ElementSample(){
        return [
            {label:'圆形',value:'circle'},
            {label:'方形',value:'rect'},
            {label:'半圆形',value:'semiCircle'},
        ]
    }
    static D2ElementItemTpl(){
        return {
            "tag": "semiCircle",
            "offsetX": 0,
            "offsetY": 0,
            "width": 200,
            "height": 50,
            "stroke": "gray",
            "border": 1,
            "fill": {
                "mode": "linear",
                "direction": "horizontal",
                "fillColor": {
                    "0": "gray",
                    "10": "gray",
                    "20": "gray",
                    "30": "gray",
                    "40": "white",
                    "50": "gray",
                    "60": "white",
                    "70": "white",
                    "80": "white",
                    "90": "white",
                    "100": "gray"
                }
            },
            "light": {
                "color": "white",
                "x": 100,
                "y": 0,
                "z": 40
            }
        }
            // {
            //     "tag": "rect",
            //     "offsetX": 0,
            //     "offsetY": 50,
            //     "width": 200,
            //     "height": 200,
            //     "stroke": "gray",
            //     "border": 1,
            //     "fill": {
            //         "mode": "linear",
            //         "direction": "horizontal",
            //         "fillColor": {
            //             "0": "gray",
            //             "40": "white",
            //             "70": "gray",
            //             "90": "white",
            //             "100": "gray"
            //         }
            //     }
            // }
        // ]
    }
    getD3GeoSample(){
        return twins.D3GeoSample();
    }
    addD3Geo(name){
        if(name==='box'){
            this.record.web.D3.geo.push({
                "name": "box",
                "type": "box",
                "width": 30,
                "height": 20,
                "depth": 20
            })
        }else if(name==='cylinder'){
            this.record.web.D3.geo.push({
                "name": "cylinder",
                "type": "cylinder",
                "top": 10,
                "bottom": 10,
                "height": 28,
                "segment": 500
            })
        }
    }
    removeD3Geo(index){
        this.record.web.D3.geo.splice(index,1);
    }
    getD3MaterialSample(){
        return twins.D3MaterialSample();
    }
    addD3Material(name){
        if(name==='lambert'){
            this.record.web.D3.material.push({
                "name": "general",
                "type": "lambert",
                "color": "#BEBEBE"
            })
        }
    }
    removeD3Material(index){
        this.record.web.D3.material.splice(index,1)
    }
    addD3Mesh(name){
        this.record.web.D3.mesh.push({
            "geo": name,
            "material": "general",
            "x": 0,
            "y": 0,
            "z": 0,
            "rx": 0,
            "ry": 0,
            "rz": 0
        })
    }
    removeD3Mesh(index){
        this.record.web.D3.mesh.splice(index,1)
    }
    static D3Tpl(){
        return {
            "name": "",
            "geo": [],
            "material": [],
            "mesh": []
        }
    }
    static D3GeoTpl(){
        return {
            "name": "box",
            "type": "box",
            "width": 30,
            "height": 20,
            "depth": 20
        }
    }
    static D3MaterialTpl(){
        return {
            "name": "general",
            "type": "lambert",
            "color": "#BEBEBE"
        }
    }
    static D3MeshTpl(){
        return {
            "geo": 'box',
            "material": "general",
            "x": 0,
            "y": 0,
            "z": 0,
            "rx": 0,
            "ry": 0,
            "rz": 0
        }
    }
    static D3GeoSample(){
        return [
            {label:'方形',value:'box'},
            {label:'圆柱',value:'cylinder'},
        ]
    }
    static D3MaterialSample(){
        return [
            {label:'朗伯材质',value:'lambert'},
        ]
    }
    static D3BoxGeoTpl(){
        return {
            "name": "box",
            "type": "box",
            "width": 30,
            "height": 20,
            "depth": 20
        }
    }
    static D3CylGeoTpl(){
        return {
            "name": "cylinder",
            "type": "cylinder",
            "top": 10,
            "bottom": 10,
            "height": 28,
            "segment": 500
        }
    }
    static D3(){
        return {
            "name": "高炉",
            "geo": [
                {
                    "name": "box",
                    "type": "box",
                    "width": 30,
                    "height": 20,
                    "depth": 20
                },
                {
                    "name": "cylinder",
                    "type": "cylinder",
                    "top": 10,
                    "bottom": 10,
                    "height": 28,
                    "segment": 500
                }
            ],
            "material": [
                {
                    "name": "general",
                    "type": "lambert",
                    "color": "#BEBEBE"
                }
            ],
            "mesh": [
                {
                    "geo": "box",
                    "material": "general",
                    "x": 0,
                    "y": 0,
                    "z": 0,
                    "rx": 0,
                    "ry": 0,
                    "rz": 0
                },
                {
                    "geo": "cylinder",
                    "material": "general",
                    "x": 0,
                    "y": 10,
                    "z": 0,
                    "rx": 0,
                    "ry": 0,
                    "rz": 180
                }
            ]
        }
    }
}

